package com.ibeeking.found.open.b.rest.service.impl;

import com.ibeeking.found.open.api.common.dto.CampaignDivideScaleDTO;
import com.ibeeking.found.open.api.common.dto.CampaignListDTO;
import com.ibeeking.found.open.api.common.dto.CampaignStatisticsDTO;
import com.ibeeking.found.open.api.common.dto.OrderShareScaleDTO;
import com.ibeeking.found.open.api.common.query.CampaignPageQuery;
import com.ibeeking.found.open.api.common.query.CampaignStatisticsQuery;
import com.ibeeking.found.open.api.feign.MarketingFeignClient;
import com.ibeeking.found.open.b.rest.service.IMarketingService;
import com.ibeeking.found.open.b.rest.service.ISystemService;
import com.ibeeking.found.open.service.common.dto.ActivityListDTO;
import com.ibeeking.found.open.service.common.dto.ActivityStatisticsDTO;
import com.ibeeking.found.open.service.common.query.ActivityListQuery;
import com.ibeeking.found.open.service.common.query.ActivityStatisticsQuery;
import com.ibeeking.found.open.service.common.query.ShareScaleQuery;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.base.page.Page;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName MarketingServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-06-04 14:21
 **/
@Service
public class MarketingServiceImpl implements IMarketingService {

    @Resource
    private MarketingFeignClient marketingFeignClient;
    @Resource
    private ISystemService systemServiceImpl;

    @Override
    public Page<ActivityListDTO> marketingList(ActivityListQuery query) {
        CampaignPageQuery campaignPageQuery = BeanUtil.convertBean(query, CampaignPageQuery.class);
        campaignPageQuery.setShopId(systemServiceImpl.queryShopIdByShopCode(query.getShopCode()));
        ResponseResult<Page<CampaignListDTO>> pageResponseResult = marketingFeignClient.campaignList(campaignPageQuery);
        if (!ResponseCodeEnum.SUCCESS.getCode().equals(pageResponseResult.getCode())) {
            throw new BusinessException("暂时查询不到活动信息");
        }
        com.ibeeking.nematos.utils.base.page.Page<ActivityListDTO> result = BeanUtil.convertBean(pageResponseResult.getData(), com.ibeeking.nematos.utils.base.page.Page.class);
        if (!Collections3.isEmpty(pageResponseResult.getData().getRecords())) {
            result.setRecords(JsonUtils.jsonToList(pageResponseResult.getData().getRecords(), ActivityListDTO.class));
        }
        return result;
    }

    @Override
    public ActivityStatisticsDTO statistics(ActivityStatisticsQuery query) {
        CampaignStatisticsQuery query1 = new CampaignStatisticsQuery();
        query1.setId(query.getId());
        query1.setShopId(systemServiceImpl.queryShopIdByShopCode(query.getShopCode()));
        ResponseResult<CampaignStatisticsDTO> responseResult = marketingFeignClient.campaignStatistics(query1);
        if (!ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode())) {
            throw new BusinessException("暂时查询不到活动信息");
        }
        return BeanUtil.convertBean(responseResult.getData(), ActivityStatisticsDTO.class);
    }

    @Override
    public List<OrderShareScaleDTO> shareScale(ShareScaleQuery query) {
        com.ibeeking.found.open.api.common.query.ShareScaleQuery query1 = new com.ibeeking.found.open.api.common.query.ShareScaleQuery();
        query1.setCampaignIds(query.getPromotionIds());
        query1.setShopId(systemServiceImpl.queryShopIdByShopCode(query.getShopCode()));
        ResponseResult<List<CampaignDivideScaleDTO>> responseResult = marketingFeignClient.campaignDivide(query1);
        if (!ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode())) {
            throw new BusinessException("暂时查询不到活动信息");
        }
        return JsonUtils.jsonToList(responseResult.getData(), OrderShareScaleDTO.class);
    }
}
